//package com.loong.forehead.gateway.filter;
//
//import com.alibaba.fastjson.JSONObject;
//
//import com.loong.common.enumerate.ResultCode;
//import com.loong.forehead.gateway.common.RequestUtils;
//
//import com.loong.forehead.gateway.handler.TokenHandler;
//import lombok.extern.slf4j.Slf4j;
//import org.springframework.cloud.gateway.filter.GatewayFilterChain;
//import org.springframework.cloud.gateway.filter.GlobalFilter;
//import org.springframework.core.Ordered;
//import org.springframework.core.io.buffer.DataBuffer;
//import org.springframework.http.MediaType;
//import org.springframework.http.server.reactive.ServerHttpRequest;
//import org.springframework.http.server.reactive.ServerHttpResponse;
//import org.springframework.util.AntPathMatcher;
//import org.springframework.util.CollectionUtils;
//import org.springframework.util.PathMatcher;
//import org.springframework.util.StringUtils;
//import org.springframework.web.server.ServerWebExchange;
//import reactor.core.publisher.Mono;
//
//import java.nio.charset.StandardCharsets;
//import java.util.List;
//
///**
// * Token验证过滤器
// *
// * 全局过滤器，默认处理所有请求的验证
// * 若需要排除验证的API请在filter/exclude.yml中指定
// */
//@Slf4j
//public class TokenFilter implements GlobalFilter, Ordered {
//
//    /**
//     * 排除签名验证的API
//     */
//    private List<String> tokenExclude;
//
//    private TokenHandler tokenHandler;
//
//    /**
//     * 路径匹配验证器
//     */
//    private PathMatcher pathMatcher = new AntPathMatcher();
//
//    public TokenFilter(List<String> tokenExclude, TokenHandler tokenHandler) {
//        this.tokenExclude = tokenExclude;
//        this.tokenHandler = tokenHandler;
//    }
//
//    @Override
//    public Mono<Void> filter(ServerWebExchange exchange, GatewayFilterChain chain) {
//        ServerHttpRequest request = exchange.getRequest();
//        if (CollectionUtils.isEmpty(tokenExclude)) {
//            return chain.filter(exchange);
//        }
//        ServerHttpResponse response = exchange.getResponse();
//        // 获取请求的路径
//        String requestPath = request.getURI().getRawPath();
//        if (check(requestPath)) {
//            return chain.filter(exchange);
//        }
//        String token = RequestUtils.getInstance().getToken(request);
//        String uid = RequestUtils.getInstance().getUid(request);
//        String osType = RequestUtils.getInstance().getOsType(request);
//        String appType = RequestUtils.getInstance().getAppType(request);
//        String deviceId = RequestUtils.getInstance().getDeviceId(request);
//        if (StringUtils.isEmpty(token) || StringUtils.isEmpty(uid) || StringUtils.isEmpty(deviceId)) {
//            return validateFail(response, ResultCode.TOEKNUNVALIBLE, null);
//        }
//        if (tokenHandler.validationToken(uid, osType, appType, token, deviceId)) {
//            return chain.filter(exchange);
//        } else {
//            // 判断是否被踢出
//            UserKickedInfo userKickedInfo = tokenHandler.getKickedInfo(token);
//            if (userKickedInfo != null) {
//                return validateFail(response, ResultCode.KICKED_OUT, userKickedInfo);
//            } else {
//                return validateFail(response, ResultCode.TOEKNUNVALIBLE, null);
//            }
//        }
//    }
//
//    @Override
//    public int getOrder() {
//        return Integer.MIN_VALUE + 2;
//    }
//
//    private Mono<Void> validateFail(ServerHttpResponse response, ResultCode code, Object data) {
//        Result result = Result.newBuilder().fail(code).addData(data).build();
//        String resultString = JSONObject.toJSONString(result);
//        response.getHeaders().setContentType(MediaType.APPLICATION_JSON);
//        DataBuffer buffer = response.bufferFactory().wrap(resultString.getBytes(StandardCharsets.UTF_8));
//        return response.writeWith(Mono.just(buffer));
//    }
//
//    private boolean check(final String requestPath) {
//        if (tokenExclude.contains(requestPath)) {
//            return true;
//        }
//        for (String excludePath : tokenExclude) {
//            if (pathMatcher.match(excludePath, requestPath)) {
//                return true;
//            }
//        }
//        return false;
//    }
//
//}
